package com.eightylegs.customer.sample;

public class Unicode {

	public static String stringToUnicode(String text) throws Exception
	{
		String unicodeText = "";
		
		for (int i = 0; i < text.length(); i++) {
			unicodeText = unicodeText.concat(charToUnicode(text.charAt(i)));
		}
		return unicodeText;
	}

	public static String charToUnicode(char c) throws Exception
	{
		int code = (int) c;
		String unicodeText = "\\u";

		if (code < 32 || code > 126)
			return unicodeText.concat(hexval(code));
		else
			return String.valueOf(c);
	}

	private static String hexval(int index) throws Exception
	{
		char[] unicodeChars = new char[4];
		unicodeChars[0]= hexdigit(index >>> 12);
		unicodeChars[1]= hexdigit(index >>> 8);
		unicodeChars[2]= hexdigit(index >>> 4);
		unicodeChars[3]= hexdigit(index);

		return new String (unicodeChars);
	}

	private static char hexdigit(int index) throws Exception
	{
		String symbols = "0123456789ABCDEF";
		return symbols.charAt(index & 0x0f);
	}

}
